package com.team4.muchentuchen;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.view.Menu;

public class myMenu extends Activity{

	WebView mWebView;
	private static final int SCAN_CODE = 0xBCDE;
    private static final int INSTALLER = 0x1401;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        
        mWebView = (WebView) findViewById(R.id.webview);    
        mWebView.getSettings().setJavaScriptEnabled(true);    
        mWebView.setWebViewClient(new HelloWebViewClient());   
        mWebView.loadUrl("http://eat-ria.appspot.com/"); //10.0.2.2:8080
    }
	
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == SCAN_CODE && resultCode == RESULT_OK) {
	            // We got a scanner response.
	            String code;
	            code = data.getStringExtra("SCAN_RESULT");
	            mWebView.loadUrl("http://eat-ria.appspot.com/diner/get_table/" + code + "/");
	            return;
	    }else{
	    	mWebView.loadUrl("http://eat-ria.appspot.com/diner/get_table/");
	    }
	    super.onActivityResult(requestCode, resultCode, data);
	}

	private class HelloWebViewClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            if (url.matches(".*zxing\\.appspot\\.com\\/scan.*")) {
	                    Intent intent;
	                    intent = new Intent("com.google.zxing.client.android.SCAN");
	                    intent.setPackage("com.google.zxing.client.android");
	                    try {
	                            startActivityForResult(intent, SCAN_CODE);
	                    } catch (ActivityNotFoundException e) {
	                            // Forward the user to the market place.
	                            intent = new Intent(Intent.ACTION_VIEW);
	                            intent.setData(Uri.parse("market://search?q=pname:com.google.zxing.client.android"));
	                            try {
	                                    startActivityForResult(intent, INSTALLER);
	                            } catch (ActivityNotFoundException x) {
	                                    Toast.makeText(
	                                                    view.getContext(),
	                                                    "No barcode scanner installed",
	                                                    Toast.LENGTH_LONG
	                                    ).show();
	                            }
	                    }
	                    return true;
	            }
	            view.loadUrl(url);
	            return true;
	    }
	}
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {    // So "back" key will go back in browser
    	if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {        
    		mWebView.goBack();        
    		return true;    
    		}    
    	return super.onKeyDown(keyCode, event);
    	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.app_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.home:
	    	 mWebView.loadUrl("http://eat-ria.appspot.com/"); //10.0.2.2:8080
	        return true;
	    case R.id.about:
	    	  openOptionsHelpDialog();
	        return true;
	    case R.id.createaccount:
	    	mWebView.loadUrl("http://eat-ria.appspot.com/create_account/");
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	 
	private void openOptionsHelpDialog(){
		new AlertDialog.Builder(this)
		.setTitle(R.string.about_title).setMessage(R.string.about_message)
		.setPositiveButton(R.string.about_ok,
				new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which){}
		 		}
		 )
		 .show();
	 }
	 
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	   // ignore orientation/keyboard change
	   super.onConfigurationChanged(newConfig);
	 }
}
